//
//  ViewController.m
//  ZQDB
//
//  Created by huanghuan on 15/9/10.
//  Copyright (c) 2015年 huanghuan. All rights reserved.
//

#import "ViewController.h"
#import "ZQModel.h"
#import "IZQDB.h"
#import "ZQBaseDao.h"
#import "ZQDeviceModel.h"
typedef void (^TestBlock)(BOOL result,int sum);//执行的block
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dict = [[NSMutableDictionary alloc] init];
    // Do any additional setup after loading the view, typically from a nib.
    NSError *error = [NSError errorWithDomain:@"error!" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:@"error!",NSLocalizedDescriptionKey, nil]];
    NSLog(@"localizedDescription:%@",error.localizedDescription);
//    [self testBlock:^(BOOL result, int sum) {
//        NSLog(@"%d",sum);
//    }];
}


-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
  //  [self testDict];
   // [self testQuery];
    ZQBaseDao *dao = [[ZQBaseDao alloc] init];
    NSString *sql1 = @"delete from device where id = 4";
    NSString *sql2 = @"delete from device where id = 1000";
    NSArray *sqlArray = [NSArray arrayWithObjects:sql1,sql2, nil];
    [[ZQDBHelper sharedInstance] execMultipleSql:sqlArray];
 
}

-(void) testQuery
{
    ZQBaseDao *dao = [[ZQBaseDao alloc] init];
    NSLog(@"======start query ====");
    NSDate *startDate = [NSDate date];
   NSArray *array =  [dao getItemsByCondStr:@"order by orders"];
    NSLog(@"====== end query");
    NSDate *endDate = [NSDate date];
     NSLog(@"query:%f",[endDate timeIntervalSinceDate:startDate]);
    
}

-(void) quickSort:(NSMutableArray *)array first:(NSInteger)first last:(NSInteger)last
{
    
    if (first < last) {
        ZQDeviceModel *keyModel = [array objectAtIndex:first];
        NSInteger left = first;
        NSInteger right = last;
        while (left < right) {
            while (left < right && ((ZQDeviceModel *)([array objectAtIndex:right])).order >= keyModel.order) { //将右边小于key的与key交换位置
                right--;
            }
            [array replaceObjectAtIndex:left withObject:[array objectAtIndex:right]];
            
            while ( left < right && ((ZQDeviceModel *)([array objectAtIndex:left])).order <= keyModel.order) { //将左边大于key的与key交换
                left++;
            }
            [array replaceObjectAtIndex:right withObject:[array objectAtIndex:left]];
        }
        [array replaceObjectAtIndex:left withObject:keyModel];
        [self quickSort:array first:first last:left-1];
        [self quickSort:array first:left+1 last:last];
        
        
    }
}

-(void) testDict
{
    for(int i = 0; i < 200; i++)
    {
        
       srand((unsigned)time(NULL));
       int Id = rand()%100;
        Id = i;
        NSLog(@"Id is:====%d",Id);
        ZQDeviceModel *deviceModel = [[ZQDeviceModel alloc] init];
        deviceModel.Id = Id;
        deviceModel.deviceType = 0x10;
        deviceModel.ver = 0x01;
        deviceModel.name = [NSString stringWithFormat:@"灯泡%d",Id];
        deviceModel.rcType = @"LIHGT";
        deviceModel.roomId = 1;
        deviceModel.addr = [NSString stringWithFormat:@"00-12-4b-00-%x",Id];
        deviceModel.icon = @"";
        [_dict setObject:deviceModel forKey:[NSNumber numberWithInt:Id]];
    }
    NSDate *startDate = [NSDate date];
    NSMutableArray *deviceArray = [NSMutableArray arrayWithArray:[_dict allValues]];
    [self quickSort:deviceArray first:0 last:deviceArray.count-1];
    NSDate *endDate = [NSDate date];
    NSLog(@"%f",[endDate timeIntervalSinceDate:startDate]);
    [self testQuery];
    ZQBaseDao *dao = [[ZQBaseDao alloc] init];
    [dao insertItems:[_dict allValues]];
}

-(void) testPredicate
{
    ZQDeviceModel *deviceModel = [[ZQDeviceModel alloc] init];
    deviceModel.Id = 1;
    deviceModel.deviceType = 0x10;
    deviceModel.ver = 0x01;
    deviceModel.name = @"设备名称";
    deviceModel.rcType = @"LIHGT";
    deviceModel.roomId = 1;
    deviceModel.addr = @"11-0d";
    deviceModel.icon = @"";
    ZQBaseDao *dao = [[ZQBaseDao alloc] init];
    
    ZQDeviceModel *deviceModel1 = [[ZQDeviceModel alloc] init];
    deviceModel1.Id = 2;
    deviceModel1.deviceType = 0x11;
    deviceModel1.ver = 0x01;
    deviceModel1.name = @"设备名称";
    deviceModel1.rcType = @"LIHGT";
    deviceModel1.roomId = 1;
    deviceModel1.icon = @"";
    deviceModel1.addr = @"11-0d";
    [dao insertItems:[NSArray arrayWithObjects:deviceModel,deviceModel1, nil]];
    NSArray *array = [NSArray arrayWithObjects:deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1, deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1, deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,deviceModel,deviceModel1,nil];
    NSLog(@"===== start =====");
    NSMutableArray *devArray = [NSMutableArray array];
    for(ZQDeviceModel *model in array){
        if ((model.deviceType == 0x11 && model.ver == 0x01)) {
            [devArray addObject:model];
        }
    }
    for(ZQDeviceModel *model in array){
        if ((model.deviceType == 0x11 && model.ver == 0x01)) {
            [devArray addObject:model];
        }
    }
    NSLog(@"====== end ======");
    NSLog(@"===== start =====");
    NSString *str = [NSString stringWithFormat:@"SELF.deviceType = %d or (SELF.deviceType = %d and SELF.ver = %d)",0x10,0x11,0x02];
    NSPredicate *pred = [NSPredicate predicateWithFormat:str];
    NSArray *deviceArray = [array filteredArrayUsingPredicate:pred];
    NSLog(@"====== end ======");
}
-(void) test:(const char **) err
{
    const char * error = "test eror";
    *err = error;
}


-(void) testBlock:(TestBlock) block
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        int sum = 0;
        for (int i = 0; i < 10; i++) {
            sum += i;
            [NSThread sleepForTimeInterval:1];
        }
        block(YES,sum);
    });

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
